/**
 * wNum=Math.ceil(stage.stageWidth/WIGHT);
 * hNum=Math.ceil(stage.stageHeight/HEIGHT);
 * 需要给这个函数  方块的个数， 横向×纵向
 * 
 * 在Event.Resize中，需要监听和改变 wNum和hNum的个数
 * 以及showMc,hideMc,maskContainer的宽高
 * protected function __resizeHandle(event:Event=null):void
	{
		maskContainer.width=stage.stageWidth;
		maskContainer.height=stage.stageHeight;
			
		wNum=Math.ceil(stage.stageWidth/WIGHT);
		hNum=Math.ceil(stage.stageHeight/HEIGHT);
			
		showBmp.width=stage.stageWidth;
		showBmp.height=stage.stageHeight;
			
		hideBmp.width=stage.stageWidth;
		hideBmp.height=stage.stageHeight;
	}
 *
 */
package com.alpheus.util
{
	import com.greensock.TweenMax;
	
	import flash.display.DisplayObject;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;

	public class MaskEffect extends Sprite
	{

		
		private static const WIGHT:int=50;
		private static const HEIGHT:int=50;

		/**
		 * 遮罩效果：
		 * @maskContainer 遮罩
		 * @showMc 需要显示的影片剪辑
		 * @hideMc 要被隐藏的影片剪辑
		 * @wNum 横向小矩形的个数
		 * @hNum 纵向小矩形的个数
		 * @type 0-3，四个方向播放
		 * @callback 回调函数
		 */
		public static function getMaskElement(maskContainer:Sprite,showMc:DisplayObject,hideMc:DisplayObject,wNum:Number,hNum:Number,type:int=0,callback:Function=null):void{

			maskContainer.scaleX=1;
			maskContainer.scaleY=1;
			
			showMc.cacheAsBitmap=true;
			maskContainer.cacheAsBitmap=true;
			showMc.mask=maskContainer;
			createNewMask(maskContainer,showMc,wNum,hNum,type,callback);

		}

		
		public static function createNewMask(maskContainer:Sprite,showMc:DisplayObject,wNum:Number,hNum:Number,type:int=0,callback:Function=null):void{
			
			var mx:Number=(wNum-5)/2;
			var my:Number=(hNum+5)/2;
			var l:Number=Math.sqrt(Math.pow((mx-i),2)+Math.pow((my-j),2));
			var rule:Number;
			
			while(maskContainer.numChildren)maskContainer.removeChildAt(0);
			
//			maskArr=[];
			for(var i:uint=0;i<hNum;i++){//控制列数
				var arr:Array=[];
				for(var j:uint=0;j<wNum;j++){//控制行数
					var mm:Shape=new Shape();
					mm.graphics.beginFill(0x000000);
					mm.graphics.drawRect(0,0,WIGHT,HEIGHT);
					mm.graphics.endFill();
					maskContainer.addChild(mm);
					mm.x=j*WIGHT;
					mm.y=i*HEIGHT;
					mm.alpha=0;
//					arr.push(mm);
					switch(type){
						case 0:
							if((i==(hNum-1))&&(j==(wNum-1)))
								TweenMax.to(mm,1,{delay:(i+j)*0.05,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
							else
								TweenMax.to(mm,1,{delay:(i+j)*0.05,alpha:1});
							break;
						case 1:
							if((i==(hNum-1))&&(j==(wNum-1-j)))
								TweenMax.to(mm,1,{delay:(i+wNum-j)*0.05,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
							else
								TweenMax.to(mm,1,{delay:(i+wNum-j)*0.05,alpha:1});
							break;
						case 2:
							if((i==(hNum-1-i))&&(j==(wNum-1)))
								TweenMax.to(mm,1,{delay:(hNum-i+j)*0.05,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
							else
								TweenMax.to(mm,1,{delay:(hNum-i+j)*0.05,alpha:1});
							break;
						case 3:
							if((i==(hNum-1-i))&&(j==(wNum-1-j)))
								TweenMax.to(mm,1,{delay:(hNum-i+wNum-j)*0.05,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
							else
								TweenMax.to(mm,1,{delay:(hNum-i+wNum-j)*0.05,alpha:1});
							break;
						case 4:
							l=Math.sqrt(Math.pow((mx-i),2)+Math.pow((my-j),2));
							if(i==0&&j==0){
								rule=l;
							}
							
							if(i==Math.ceil(mx)&&j==Math.ceil(my)){
								TweenMax.to(mm,1,{delay:(rule-l)*0.15,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
							}else{
								TweenMax.to(mm,1,{delay:(rule-l)*0.15,alpha:1});
							}
							break;
						case 5:
							l=Math.sqrt(Math.pow((mx-i),2)+Math.pow((my-j),2));
							if(i==0&&j==0){
								rule=l;
							}
							
							if(i==0&&j==0){
								TweenMax.to(mm,1,{delay:l*0.15,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
							}else{
								TweenMax.to(mm,1,{delay:l*0.15,alpha:1});
							}
							break;
					}
					if((i==(hNum-1))&&(j==(wNum-1-j)))
						TweenMax.to(mm,1,{delay:(i+wNum-j)*0.05,alpha:1,onComplete:runFinish,onCompleteParams:[maskContainer,showMc,callback]});
					else
						TweenMax.to(mm,1,{delay:(i+wNum-j)*0.05,alpha:1});
				}
//				maskArr.push(arr);
			}
		}
		
		
		/**
		 * 播放完成
		 */
		private static function runFinish(maskContainer:Sprite,showMc:DisplayObject,callback:Function=null):void{
			trace(".0.");
			showMc.mask=null;
			while(maskContainer.numChildren)maskContainer.removeChildAt(0);
			if(callback!=null){
				callback();
			}
		}
	}
}